<?php

namespace app\api\controller;

use app\common\model\AdminModel;
use Firebase\JWT\JWT;
use think\Controller;
use think\Request;

class Login extends Cross
{
    //登录方法
    public function login(Request $request)
    {
        $username = $request->param('username');
        $password = $request->param('password');
        $admin = new AdminModel();
        $info = $admin->where('username',$username)->find();
        if(!$info){
                    return json(['code'=>0,'msg'=>'帐号不存在']);

        }

        if($info['password'] != md5($password)){
                    return json(['code'=>0,'msg'=>'帐号或密码错误']);

        }

        $jwt = new JWT();
        $key = 'api123456';
        $payload = [
            'iss' => "http://api.io",
            'aud' => "http://api.io",
            'iat' => time(),
            'nbf' => time(),
            'aid' => $info['id']
        ];

        $token = $jwt::encode($payload,$key);

        return json(['code'=>1,'msg'=>'登陆成功','token'=>$token]);




    }
    
}
